package com.topvision.elementanim.hualang;

import android.graphics.Point;
import android.media.MediaPlayer;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Display;
import android.view.KeyCharacterMap;
import android.view.KeyEvent;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.topvision.elementanim.R;
import com.topvision.videolibs.UniversalVideoView;

import java.io.File;

public class VideoViewerActivity extends AppCompatActivity implements MediaPlayer.OnCompletionListener {

    private static final String SEEK_POSITION_KEY = "SEEK_POSITION_KEY";
    private UniversalVideoView mVideoView;
    private ImageView mThumbnail;
    private int mSeekPosition;
    private String res;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_viewer);
        res = getIntent().getStringExtra(Constants.RES_IMAGE);
        initView();
        initData();
    }

    private void initView() {
        mVideoView = (UniversalVideoView) findViewById(R.id.videoView);
    }

    private void initData() {
//        setMediaControllerHeight();
        mVideoView.setOnCompletionListener(this);
        if (prepareVideo()) {
            mVideoView.start();
        }
    }

    private boolean prepareVideo() {
        File file = new File(res);
        if (file.exists() && file.isFile()) {
            mVideoView.setVideoPath(res);
            mVideoView.requestFocus();
            return true;
        }
        return false;
    }

    private void setMediaControllerHeight() {
        int barHeight = getNavigationBarHeight();
        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT);
        layoutParams.bottomMargin = barHeight;
//        mMediaController.setLayoutParams(layoutParams);
    }

    public boolean isNavigationBarShow() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            Display display = getWindowManager().getDefaultDisplay();
            Point size = new Point();
            Point realSize = new Point();
            display.getSize(size);
            display.getRealSize(realSize);
            return realSize.y != size.y;
        } else {
            boolean menu = ViewConfiguration.get(this).hasPermanentMenuKey();
            boolean back = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK);
            if (menu || back)
                return false;
            else
                return true;

        }
    }

    public int getNavigationBarHeight() {
        if (!isNavigationBarShow()) {
            return 0;
        }
        int resourceId = getResources().getIdentifier("navigation_bar_height",
                "dimen", "android");
        //获取NavigationBar的高度
        return getResources().getDimensionPixelSize(resourceId);
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (mVideoView != null && mVideoView.isPlaying()) {
            mSeekPosition = mVideoView.getCurrentPosition();
            mVideoView.pause();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mVideoView != null) {
            mVideoView.seekTo(mSeekPosition);
            mVideoView.start();
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt(SEEK_POSITION_KEY, mSeekPosition);
    }

    @Override
    protected void onRestoreInstanceState(Bundle outState) {
        super.onRestoreInstanceState(outState);
        mSeekPosition = outState.getInt(SEEK_POSITION_KEY);
    }

    @Override
    public void onCompletion(MediaPlayer mp) {

    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        int i = 4 / 0;
    }
}
